package com.nyist.Controller;

import java.util.Optional;

import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nyist.entity.GoodsInfo;
import com.nyist.repository.GoodsRepository;

@RestController
@RequestMapping("/es")
public class EsControllerr {

	@Autowired
	private GoodsRepository repository;
	
	@RequestMapping("test")
	public void test() {
		NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
		nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("name","商品1548992453631"));
	    Page<GoodsInfo> users = repository.search(nativeSearchQueryBuilder.build());
	    for (GoodsInfo goodsInfo : users) {
			System.out.println(goodsInfo);
		}	
	}
	
	@RequestMapping("save")
	public String save(GoodsInfo goods) {
		repository.save(goods);
		return "success";
	}
	
	@GetMapping("delete")
	public String deleteById(Long id) {
		repository.deleteById(id);
		return "success";
	}
	
	@RequestMapping("findById")
	public GoodsInfo getById(Long id) {
		Optional<GoodsInfo> info = repository.findById(id);
		GoodsInfo goods = info.get();
		return goods;
	}
	
	
}
